﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ITD_VAPCF.NganQuy.ENTITY;
using ITD_VAPCF.NganQuy.BLL;
using ITD_VAPCF.NganQuy.REPORT;

namespace ITD_VAPCF.NganQuy.GUI
{
    public partial class frmKKTM : DevComponents.DotNetBar.Office2007Form
    {
        private DataTable _dtTable = new DataTable();
        private string _giaoDichID = "";
        private KKTienMatBLL objNQ_GiaoDich = new KKTienMatBLL(CommonVAPCF.GlobalVariables.g_strConnectionMsSql);
        private KKTienMat _entityTienMat = new KKTienMat();
        private TTKhachHang _entityKhachHang = new TTKhachHang();
        private TienMatGiaoDich _entityGiaoDich = new TienMatGiaoDich();
        private CrystalDecisions.CrystalReports.Engine.ReportClass report;
        public DataTable DtTable
        {
            get { return _dtTable; }
            set
            {
                _dtTable = value;
            }
        }
        private bool _oneGate = false;
        private bool _release = true;
        public bool Release
        {
            get { return _release; }
            set { _release = value; }
        }
        public bool OneGate
        {
            get { return _oneGate; }
            set
            {
                _oneGate = value;
                pnlTop.Visible = !value;
                btnIn.Visible = !value;
            }
        }

        public string GiaoDichID
        {
            get { return _giaoDichID; }
            set
            {
                _giaoDichID = value;
                objNQ_GiaoDich = new KKTienMatBLL(CommonVAPCF.GlobalVariables.g_strConnectionMsSql);
                DataTable dt = objNQ_GiaoDich.SelectOnlyOneEntityByID(value);
                LoadNhapBangKe();
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < _dtTable.Rows.Count; i++)
                    {
                        for (int j = 0; j < dt.Rows.Count; j++)
                        {
                            if (_dtTable.Rows[i]["loai_tien_id"].ToString() == dt.Rows[j]["loai_tien_id"].ToString())
                            {
                                _dtTable.Rows[i]["TonDauKy"] = dt.Rows[j]["TonDauKy"];
                                _dtTable.Rows[i]["TonCuoiKy"] = dt.Rows[j]["TonDauKy"];
                                _dtTable.Rows[i]["Thu"] = dt.Rows[j]["Thu"];
                                _dtTable.Rows[i]["Chi"] = dt.Rows[j]["Chi"];
                            }
                        }
                    }
                    lblNguoiDuyet.Text = dt.Rows[0]["UsersModify"].ToString();
                    lblNguoiNhap.Text = dt.Rows[0]["FullName"].ToString();
                    lblNgayDuyet.Text = dt.Rows[0]["DateModify"].ToString();
                    if (lblNgayDuyet.Text == "") lblTrangThai.Text = "Đang nhập";
                    else lblTrangThai.Text = "Đã duyệt";
                    dtgvBangKe.AutoGenerateColumns = false;
                    dtgvBangKe.DataSource = _dtTable.DefaultView;

                    ShowEdit();
                    loai_tien_id.Visible = false;
                }
            }
        }
        public KKTienMat EntityTienMat
        {
            get { return _entityTienMat; }
            set { _entityTienMat = value; }
        }
        public DateTime NgayGiaoDich
        {
            set { txt_ngayGD.Value = value; }
            get { return txt_ngayGD.Value; }
        }
        public TTKhachHang EntityKhachHang
        {
            get
            {
                _entityKhachHang.TenKhachHang = txtTenKH.Text;
                _entityKhachHang.DiaChi = txtDiaChi.Text;
                _entityKhachHang.MaKhachHang = txtMaKH.Text;
                _entityKhachHang.SoDienThoai = txtDienThoai.Text;
                _entityKhachHang.CMT = txtSoCMND.Text;
                _entityKhachHang.CmtNoiCap = txtNoiCap.Text;
                _entityKhachHang.CmtNgayCap = txtNgayCap.Text;
                _entityKhachHang.NoiDung = txtNoiDungGD.Text;
                return _entityKhachHang;
            }
            set
            {
                _entityKhachHang = value;
                txtTenKH.Text = value.TenKhachHang;
                txtDiaChi.Text = value.DiaChi;
                txtMaKH.Text = value.MaKhachHang;
                txtDienThoai.Text = value.SoDienThoai;
                txtSoCMND.Text = value.CMT;
                txtNoiCap.Text = value.CmtNoiCap;
                txtNgayCap.Text = value.CmtNgayCap;
                txtNoiDungGD.Text = value.NoiDung;
            }
        }

        public TienMatGiaoDich EntityGiaoDich
        {
            get { return _entityGiaoDich; }
            set
            {
                _entityGiaoDich = value;
                txtMaGiaoDich.Text = value.GiaoDichID;
                _giaoDichID = value.GiaoDichID;
                txt_ngayGD.Value = value.NgayGiaoDich;
                cbo_LoaiGiaoDich.SelectedValueWithDataSource = value.LoaiGiaoDichID;
                cbo_LoaiGiaoDich.ReadOnly = true;
                if (value.LoaiChungTu == "01")
                {
                    txtSoTienThu.Double = value.SoTien;
                }
                else
                {
                    txtSoTienChi.Double = value.SoTien;
                }
            }
        }

        public void LoadNhapBangKe()
        {
            DtTable = NgayQuyBLL.Get_LoaiTien(Sys_Infor_SectionWorking.g_WorkingDate.ToString("yyyyMMdd"), Sys_Infor_Company.g_sBranchCode);
            ConfigDataGrid();
            ShowEdit();
        }
        public void ConfigDataGrid()
        {
            dtgvBangKe.AutoGenerateColumns = false;
            dtgvBangKe.MultiSelect = true;
            dtgvBangKe.DataSource = _dtTable.DefaultView;
        }

        public double SumThu
        {
            get { return txtSoTienThu.Double; }
            set
            {
                txtSoTienThu.Double = value;
            }
        }
        public double SumChi
        {
            get { return txtSoTienChi.Double; }
            set { txtSoTienChi.Double = value; }
        }
        public frmKKTM()
        {
            InitializeComponent();
            f_loaitien.SelectedIndex = 0;
            cbo_LoaiGiaoDich.SetDataSource(ref CommonTable.dtDm_Loai_Giao_Dich, "ma", "ten");

        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            DtTable = new DataTable();
            this.Close();
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            InChungTu();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void DoFilter()
        {
            switch (f_loaitien.SelectedIndex)
            {
                case 0:
                    _dtTable.DefaultView.RowFilter = "";
                    break;
                case 1:
                    _dtTable.DefaultView.RowFilter = "substring(loai_tien_id,1,1) = 1";
                    break;
                case 2:
                    _dtTable.DefaultView.RowFilter = "substring(loai_tien_id,1,1) = 2";
                    break;
                case 3:
                    _dtTable.DefaultView.RowFilter = "substring(loai_tien_id,1,1) = 3";
                    break;
                default:
                    break;
            }
        }

        private void dtgvBangKe_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                dtgvBangKe.EndEdit();

                if (dtgvBangKe[0, e.RowIndex].Value.ToString().Length < 3)
                {

                    dtgvBangKe["Thu", e.RowIndex].Value = DBNull.Value;
                    dtgvBangKe["Chi", e.RowIndex].Value = DBNull.Value;
                    return;
                }

                double dThu = 0;
                double dChi = 0;
                double dTonDK = 0;
                dTonDK = CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["TonDauKy", e.RowIndex].Value);
                dThu = CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["Thu", e.RowIndex].Value);
                dChi = CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["Chi", e.RowIndex].Value);
                dtgvBangKe.Rows[e.RowIndex].Cells["TonCuoiKy"].Value = dTonDK + dThu - dChi;
                string MenhGia = CommonVAPCF.FuncUtilities.DecodeMoney(dtgvBangKe["loai_tien", e.RowIndex].Value.ToString(), ",");
                double Sum = CommonVAPCF.ConvertData.ToDouble(MenhGia) * (dTonDK + dThu - dChi);
                if (Sum >= 0)
                    dtgvBangKe.Rows[e.RowIndex].Cells["SoTien"].Value = Sum;
                double TongTienThu = CommonVAPCF.ConvertData.ToDouble(MenhGia) * dThu;
                if (dThu >= 0)
                    dtgvBangKe.Rows[e.RowIndex].Cells["TongTienThu"].Value = TongTienThu;
                double TongTienChi =  CommonVAPCF.ConvertData.ToDouble(MenhGia) * dChi;
                if (dChi >= 0)
                    dtgvBangKe.Rows[e.RowIndex].Cells["TongTienChi"].Value = TongTienChi;
                TinhToan(e.RowIndex);

            }
            catch (Exception ex)
            {
                CommonVAPCF.LogFile.WriteLog(ex, this.Name, "dtgvBangKe_CellEndEdit");
                CommonVAPCF.MessagesReturn.ShowDialog("Lỗi : " + ex.ToString());
            }
        }
        private void f_loaitien_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_dtTable != null)
                DoFilter();
        }
        private void dtgvSoTietKiem_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            TextBox txtbox = e.Control as TextBox;
            if (txtbox != null)
            {
                txtbox.KeyPress += new KeyPressEventHandler(txtbox_KeyPress);
                txtbox.TextChanged += new EventHandler(txtbox_TextChanged);
            }
        }
        private void txtbox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && !(e.KeyChar == '.'))
                e.Handled = true;
        }
        private void txtbox_TextChanged(object sender, EventArgs e)
        {
            TextBox txtbox = sender as TextBox;
            string TextTmp = txtbox.Text.Trim();
            txtbox.Text = CommonVAPCF.FuncUtilities.EncodeMoney(CommonVAPCF.FuncUtilities.DecodeMoney(TextTmp, ","), ",");
            txtbox.SelectionStart = 20;
        }
        private void TinhToan()
        {
            double Sum1 = 0, Sum2 = 0, Sum3 = 0, Sum4 = 0, Sum5 = 0, Sum6 = 0;


            for (int i = 0; i < dtgvBangKe.Rows.Count; i++)
            {
                Sum1 += CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["Thu", i].Value);
                Sum2 += CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["TongTienThu", i].Value);
                Sum3 += CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["Chi", i].Value);
                Sum4 += CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["TongTienChi", i].Value);
                Sum5 += CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["TonCuoiKy", i].Value);
                Sum6 += CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["SoTien", i].Value);
            }

            txtSoToThu.Double = Sum1;
            txtTongTienThu.Double = Sum2;
            txtSoToChi.Double = Sum3;
            txtTongTienChi.Double = Sum4;
            txtSoToTonQuy.Double = Sum5;
            txtTongTienTonQuy.Double = Sum6;
            txtChenhLechChi.Text = "";
            txtChenhLechThu.Text = "";
            if (txtTongTienChi.Double > txtTongTienThu.Double)
                txtChenhLechChi.Text = (txtTongTienChi.Double - txtTongTienThu.Double).ToString("###,###,###,###");
            if (txtTongTienThu.Double > txtTongTienChi.Double)
                txtChenhLechThu.Text = (txtTongTienThu.Double - txtTongTienChi.Double).ToString("###,###,###,###");
            KiemTraSoTo();

        }
        private void TinhToan(int i)
        {
            double Sum1 = 0, Sum2 = 0, Sum3 = 0, Sum4 = 0, Sum5 = 0, Sum6 = 0;


            Sum1 = CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["Thu", i].Value) - cellValue[0];
            Sum2 = CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["TongTienThu", i].Value) - cellValue[1];
            Sum3 = CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["Chi", i].Value) - cellValue[2];
            Sum4 = CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["TongTienChi", i].Value) - cellValue[3];
            Sum5 = CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["TonCuoiKy", i].Value) - cellValue[4];
            Sum6 = CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["SoTien", i].Value) - cellValue[5];


            txtSoToThu.Double += Sum1;
            txtTongTienThu.Double += Sum2;
            txtSoToChi.Double += Sum3;
            txtTongTienChi.Double += Sum4;
            txtSoToTonQuy.Double += Sum5;
            txtTongTienTonQuy.Double += Sum6;
            txtChenhLechChi.Text = "";
            txtChenhLechThu.Text = "";
            if (txtTongTienChi.Double > txtTongTienThu.Double)
                txtChenhLechChi.Text = (txtTongTienChi.Double - txtTongTienThu.Double).ToString("###,###,###,###");
            if (txtTongTienThu.Double > txtTongTienChi.Double)
                txtChenhLechThu.Text = (txtTongTienThu.Double - txtTongTienChi.Double).ToString("###,###,###,###");
            KiemTraSoTo();

        }
        private void ShowEdit()
        {
            double Sum1 = 0, Sum2 = 0, Sum3 = 0, Sum4 = 0, Sum5 = 0, Sum6 = 0;
            double dThu = 0;
            double dChi = 0;
            double dTonDK = 0;

            for (int i = 0; i < dtgvBangKe.Rows.Count; i++)
            {
                dTonDK = CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["TonDauKy", i].Value);
                dThu = CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["Thu", i].Value);
                dChi = CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["Chi", i].Value);
                dtgvBangKe.Rows[i].Cells["TonCuoiKy"].Value = dTonDK + dThu - dChi;
                string MenhGia = CommonVAPCF.FuncUtilities.DecodeMoney(dtgvBangKe["loai_tien", i].Value.ToString(), ",");
                double Sum = CommonVAPCF.ConvertData.ToDouble(MenhGia) * (dTonDK + dThu - dChi);
                if (Sum >= 0)
                    dtgvBangKe.Rows[i].Cells["SoTien"].Value = Sum;
                if (CommonVAPCF.ConvertData.ToDouble(MenhGia) != 0)
                {
                    if (dThu >= 0)
                    {
                        dtgvBangKe["TongTienThu", i].Value = CommonVAPCF.ConvertData.ToDouble(MenhGia) * dThu;

                    }
                    if (dChi >= 0)
                        dtgvBangKe["TongTienChi", i].Value = CommonVAPCF.ConvertData.ToDouble(MenhGia) * dChi;
                }

                Sum1 += CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["Thu", i].Value);
                Sum2 += CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["TongTienThu", i].Value);
                Sum3 += CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["Chi", i].Value);
                Sum4 += CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["TongTienChi", i].Value);
                Sum5 += CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["TonCuoiKy", i].Value);
                Sum6 += CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["SoTien", i].Value);

                KiemTraSoTo();
                TinhPhiDoiTien();
            }
            dtgvBangKe.Refresh();
            txtSoToThu.Double = Sum1;
            txtTongTienThu.Double = Sum2;
            txtSoToChi.Double = Sum3;
            txtTongTienChi.Double = Sum4;
            txtSoToTonQuy.Double = Sum5;
            txtTongTienTonQuy.Double = Sum6;
            txtChenhLechChi.Text = "";
            txtChenhLechThu.Text = "";
            if (txtTongTienChi.Double > txtTongTienThu.Double)
                txtChenhLechChi.Text = (txtTongTienChi.Double - txtTongTienThu.Double).ToString("###,###,###,###");
            if (txtTongTienThu.Double > txtTongTienChi.Double)
                txtChenhLechThu.Text = (txtTongTienThu.Double - txtTongTienChi.Double).ToString("###,###,###,###");
        }
        private void KiemTraSoTo()
        {
            if (txtSoTienThu.Double > 0)
            {
                double tmpThu = txtSoTienThu.Double - txtTongTienThu.Double + txtTongTienChi.Double;
                if (tmpThu > 0)
                {
                    lbSttus.Text = "Số tiền nhập chưa đủ";
                    lbTienThua.Text = "Số tiền còn thiếu";
                    txt_TienConThua.Text = Math.Abs(tmpThu).ToString("###,###,###,###");
                }
                else if (tmpThu == 0)
                {
                    lbSttus.Text = "Số tiền đã đủ";
                    lbTienThua.Text = "Số tiền đã đủ";
                    txt_TienConThua.Text = "";
                }
                else
                {
                    lbSttus.Text = "Số tờ tiền nhiều hơn số tiền giao dịch";
                    lbTienThua.Text = "Số tiền trả lại";
                    txt_TienConThua.Text = Math.Abs(tmpThu).ToString("###,###,###,###");
                }
            }
            if (txtSoTienChi.Double > 0)
            {
                double tmpChi = txtSoTienChi.Double - txtTongTienChi.Double + txtTongTienThu.Double;
                if (tmpChi > 0)
                {
                    lbSttus.Text = "Số lượng tiền nhập chưa đủ";
                    lbTienThua.Text = "Số tiền còn thiếu";
                    txt_TienConThua.Text = Math.Abs(tmpChi).ToString("###,###,###,###");
                }
                else if (tmpChi == 0)
                {
                    lbSttus.Text = "Số tiền đã đủ";
                    lbTienThua.Text = "Số tiền đã đủ";
                    txt_TienConThua.Text = "";
                }
                else
                {
                    lbSttus.Text = "Số tờ tiền nhiều hơn số tiền giao dịch";
                    lbTienThua.Text = "Số tiền trả lại";
                    txt_TienConThua.Text = Math.Abs(tmpChi).ToString("###,###,###,###");
                }
            }
        }
        public void InChungTu()
        {
            report = new BangKeLoaiTienThu();
            {
                DataTable tmp = DtTable.DefaultView.ToTable(true, "loai_tien_id", "loai_tien"
                                                            , "Thu", "TongTienThu", "Chi", "TongTienChi");
                DataRow[] rows = DtTable.Select("len(loai_tien_id)<3");
                tmp.Columns[0].ColumnName = "LoaiTien";
                tmp.Columns[1].ColumnName = "MenhGia";
                tmp.Columns[2].ColumnName = "A2";// Thu
                tmp.Columns[3].ColumnName = "A3";// Tổng tiền thu
                tmp.Columns[4].ColumnName = "A1";// Chi
                tmp.Columns[5].ColumnName = "A4";// Tổng tiền chi
                for (int i = 0; i < tmp.Rows.Count; i++)
                {
                    tmp.Rows[i]["LoaiTien"] = tmp.Rows[i]["LoaiTien"].ToString().Substring(0, 1);
                }
                for (int i = 0; i < tmp.Rows.Count; i++)
                {
                    for (int j = 0; j < rows.Length; j++)
                    {
                        if (tmp.Rows[i]["LoaiTien"].ToString() == rows[j][0].ToString())
                            tmp.Rows[i]["loaitien"] = rows[j][1].ToString();
                    }
                }

                tmp.DefaultView.RowFilter = "A3>0 or A4 >0";
                report.SetDataSource(tmp.DefaultView);
                report.SetParameterValue("Ten_Don_vi", Sys_Infor_Company.g_sCompanyName.ToUpper());
                report.SetParameterValue("Dia_chi_Don_vi", Sys_Infor_Company.g_sCompanyAddress);
                report.SetParameterValue("User_id", Common.GlobalUser.gs_UserName);
                report.SetParameterValue("Mau_so", "11/KT");
                report.SetParameterValue("Ten_BC", "BẢNG KÊ LOẠI TIỀN");
                report.SetParameterValue("Ten_Khach_hang", EntityKhachHang.TenKhachHang);
                report.SetParameterValue("Dia_chi", EntityKhachHang.DiaChi);
                report.SetParameterValue("Di_dong", EntityKhachHang.SoDienThoai);
                report.SetParameterValue("Noi_dung", EntityKhachHang.NoiDung);
                report.SetParameterValue("Tien_Bang_Chu", CommonVAPCF.Functions.MoneyToString((Math.Abs(txtTongTienThu.Double - txtTongTienChi.Double)).ToString(), true));
                report.SetParameterValue("Ngay_thang_nam", txt_ngayGD.Value.ToString("dd/MM/yyyy"));
                if (txtSoToThu.Text != "" && txtSoToThu.Text != "0")
                {
                    report.SetParameterValue("Nguoi_nop_tien", "Người nộp tiền");
                    report.SetParameterValue("Nguoi_thu_tien", "Người thu tiền");
                }
                else
                {
                    report.SetParameterValue("Nguoi_nop_tien", "Người nhận tiền");
                    report.SetParameterValue("Nguoi_thu_tien", "Người chi tiền");
                }
                report.SetParameterValue("Ten_khach_hang", EntityKhachHang.TenKhachHang);
                report.SetParameterValue("Giao_dich_vien", lblNguoiNhap.Text);

                CommonVAPCF.frmPrintPreview frm = new CommonVAPCF.frmPrintPreview();
                frm.LoadDataPrint(report);
                frm.ShowDialog();
            }

        }
        public void Save()
        {
            objNQ_GiaoDich.Delete_ThongTinKhachHang(GiaoDichID);
            objNQ_GiaoDich.InsUpd_ThongTinKhachHang(GiaoDichID, _entityKhachHang, "Ins");
            _entityTienMat = new KKTienMat();
            _entityTienMat.GiaoDichID = _entityGiaoDich.GiaoDichID;
            _entityTienMat.Ngay = txt_ngayGD.Value.Date;
            _entityTienMat.SoTien = _entityGiaoDich.SoTien;
            objNQ_GiaoDich.Delete(GiaoDichID);
            for (int i = 0; i < dtgvBangKe.Rows.Count; i++)
            {
                double dThu = 0, dChi = 0;
                if (dtgvBangKe["Thu", i].Value != null) dThu = CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["Thu", i].Value);
                if (dtgvBangKe["Chi", i].Value != null) dChi = CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["Chi", i].Value);
                if (dThu + dChi > 0)
                {
                    _entityTienMat.Loai_Giao_Dich = _entityGiaoDich.LoaiGiaoDichID;
                    _entityTienMat.LoaiTienID = dtgvBangKe["loai_tien_id", i].Value.ToString();
                    _entityTienMat.SoChungTu = _entityGiaoDich.SoChungTu;

                    _entityTienMat.Thu = dThu;
                    _entityTienMat.Chi = dChi;
                    _entityTienMat.TienTe = "00";
                    _entityTienMat.TonCuoiKy = CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["TonCuoiKy", i].Value);
                    _entityTienMat.TonDauKy = CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["TonDauKy", i].Value);
                    _entityTienMat.UsersNew = Common.GlobalUser.gs_UserName;
                    _entityTienMat.UsersModify = Common.GlobalUser.gs_UserName;
                    _entityTienMat.ChiNhanh_ID = Sys_Infor_Company.g_sBranchCode;
                    if (GiaoDichID != "")
                    {
                        objNQ_GiaoDich.Insert(_entityTienMat);
                    }
                }
            }
            if (!OneGate)
            {
                CommonVAPCF.MessagesReturn.ShowDialog("Đã cập nhật xong");
                GiaoDichID = _giaoDichID;
            }
        }

        private void frmKKTM_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if ((e.KeyCode == Keys.Escape) || (e.KeyCode == Keys.F10))
                    this.Close();
            }
            catch
            {
            }
        }
        private double[] cellValue = new double[6];
        private void dtgvBangKe_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            cellValue[0] = CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["Thu", e.RowIndex].Value);
            cellValue[1] = CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["TongTienThu", e.RowIndex].Value);
            cellValue[2] = CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["Chi", e.RowIndex].Value);
            cellValue[3] = CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["TongTienChi", e.RowIndex].Value);
            cellValue[4] = CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["TonCuoiKy", e.RowIndex].Value);
            cellValue[5] = CommonVAPCF.ConvertData.ToDouble(dtgvBangKe["SoTien", e.RowIndex].Value);
        }

        private void dtgvBangKe_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rect = dtgvBangKe.GetColumnDisplayRectangle(2, false);
            txtTongCong.Size = new Size(rect.Width + 16, txtTongCong.Size.Height);

            rect = dtgvBangKe.GetColumnDisplayRectangle(3, false);
            txtSoToThu.Location = new Point(rect.X, 0);
            txtSoToThu.Size = new Size(rect.Width, txtSoToThu.Size.Height);

            rect = dtgvBangKe.GetColumnDisplayRectangle(4, false);
            txtTongTienThu.Location = new Point(rect.X, 0);
            txtTongTienThu.Size = new Size(rect.Width, txtTongTienThu.Size.Height);

            rect = dtgvBangKe.GetColumnDisplayRectangle(5, false);
            txtSoToChi.Location = new Point(rect.X, 0);
            txtSoToChi.Size = new Size(rect.Width, txtSoToChi.Size.Height);

            rect = dtgvBangKe.GetColumnDisplayRectangle(6, false);
            txtTongTienChi.Location = new Point(rect.X, 0);
            txtTongTienChi.Size = new Size(rect.Width, txtTongTienChi.Size.Height);

            rect = dtgvBangKe.GetColumnDisplayRectangle(7, false);
            txtSoToTonQuy.Location = new Point(rect.X, 0);
            txtSoToTonQuy.Size = new Size(rect.Width, txtSoToTonQuy.Size.Height);

            rect = dtgvBangKe.GetColumnDisplayRectangle(8, false);
            txtTongTienTonQuy.Location = new Point(rect.X, 0);
            txtTongTienTonQuy.Size = new Size(rect.Width, txtTongTienTonQuy.Size.Height);
            string MenhGia = CommonVAPCF.FuncUtilities.DecodeMoney(dtgvBangKe["loai_tien", e.RowIndex].Value.ToString(), ",");
            if (CommonVAPCF.ConvertData.ToDouble(MenhGia) == 0)
            {
                dtgvBangKe.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.PaleGoldenrod;
                dtgvBangKe.Rows[e.RowIndex].ReadOnly = true;
                dtgvBangKe.Rows[e.RowIndex].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
            }
            else
                dtgvBangKe.Rows[e.RowIndex].DefaultCellStyle.Format = "###,###,###,###";
        }

        private void dtgvBangKe_Paint(object sender, PaintEventArgs e)
        {
            if (!_release)
            {
                Font _front = new System.Drawing.Font("Microsoft Sans Serif", 18F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold))), System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                string strRowNumber = "CHỨC NĂNG NÀY THUỘC PHÂN HỆ NGÂN QUỸ, \r\n ĐƠN VỊ BẠN CHƯA ĐƯỢC CUNG CẤP \r\n BẠN LIÊN HỆ VỚI VĂN PHÒNG CÔNG TY ĐỂ ĐĂNG KÝ SỬ DỤNG";
                Brush b = Brushes.Blue;
                e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                e.Graphics.DrawString(strRowNumber, _front, b, e.ClipRectangle.Left + 30, e.ClipRectangle.Top + 200);
            }
            Image imgDown = global::ITD_VAPCF.NganQuy.Properties.Resources.Stock_Index_Down;
            Image imgUp = global::ITD_VAPCF.NganQuy.Properties.Resources.Stock_Index_Up;
            Image imgMoney = global::ITD_VAPCF.NganQuy.Properties.Resources.money;
            int left = 0;
            int top = 0;
            Rectangle rect = dtgvBangKe.GetColumnDisplayRectangle(3, false);
            left = rect.Right - 20;
            top = rect.Top + 10;
            e.Graphics.DrawImage(imgUp, left, top);
            rect = dtgvBangKe.GetColumnDisplayRectangle(5, false);
            left = rect.Right - 20;
            top = rect.Top + 10;
            e.Graphics.DrawImage(imgDown, left, top);

            rect = dtgvBangKe.GetColumnDisplayRectangle(2, false);
            left = rect.Right - 30;
            top = rect.Top + 10;
            e.Graphics.DrawImage(imgMoney, left, top);
        }

        private void frmKKTM_Shown(object sender, EventArgs e)
        {
            dtgvBangKe.Enabled = _release;
            btnSave.Enabled = _release;
            btnIn.Enabled = _release;
        }

        private void TinhPhiDoiTien()
        {
            try
            {
                double num = Common.ConvertData.ToDouble(this.txtTongTien.Text.Trim());
                double num2 = Common.ConvertData.ToDouble(this.txtTyLeTinhPhi.Text.Trim());
                double num3 = (num * num2) / 100;
                this.txtPhiPhaiTra.Text = num3.ToString("###,###,###");
                double num4 = Common.ConvertData.ToDouble(this.txtTyLeThueVAT.Text.Trim());
                double num5 = (num3 * num4) / 100;
                this.txtThueVAT.Text = num5.ToString("###,###,###");
                this.txtTongCongPhi.Text = (num3 + num5).ToString("###,###,###");
            }
            catch (Exception exception)
            {

            }
        }

        private void txtTyLeTinhPhi_TextChanged(object sender, EventArgs e)
        {
            TinhPhiDoiTien();
        }

        private void xóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnSave_Click(null, null);
        }

        private void xóaToolStripMenuItem1_Click(object sender, EventArgs e)
        {

            for (int i = 0; i < dtgvBangKe.Rows.Count; i++)
            {
                if (dtgvBangKe["Thu", i].Selected)
                {
                    dtgvBangKe["Thu", i].Value = 0;
                }
                if (dtgvBangKe["Chi", i].Selected)
                {
                    dtgvBangKe["Chi", i].Value = 0;
                }
            }
            ShowEdit();
        }

        private void thoátToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnThoat_Click(null, null);
        }
    }
}
